package AddressService

import (
	"context"
	"gdshop-font-api-go/app/request/AddressReq"
	"gdshop-font-api-go/app/response/AddressResp"
	"gdshop-font-api-go/library/response"
	"gdshop-font-api-go/library/tools"
	toolsDb "gdshop-font-api-go/library/tools/db"
)

func List(ctx context.Context, parames *AddressReq.List) *response.JsonResponse {
	addressList := []*AddressResp.Item{}
	count, countErr := toolsDb.GetUnSafaTableAddDeleteWhere(ctx,
		"member_address").Where("delete_at<1 AND member_id=?", parames.MemberId).Fields(
		"1").Count()
	if countErr != nil {
		return response.FailByRequestMessage(nil, countErr.Error())
	}

	err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx,
		"member_address").Where("member_id=?", parames.MemberId).Page(
		parames.Page, parames.PageSize,
	).Structs(&addressList)

	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	} else {
		addressRespList := AddressResp.List{
			PageInfo: tools.GetPageInfo(count, parames.Page, parames.PageSize),
		}
		addressRespList.List = addressList
		return response.SuccessByRequestMessageData(nil, "成功", addressRespList)
	}
}
